/**
 * 
 */
package net.ddns.heroli.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.gzheroli.bfw.pojo.base.TSUser;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.model.json.AjaxJson;
import cn.com.gzheroli.core.common.model.json.TreeGrid;
import cn.com.gzheroli.core.common.service.CommonService;
import cn.com.gzheroli.core.common.service.HeroService;
import cn.com.gzheroli.core.common.service.SystemService;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.util.NameHandler;
import cn.com.gzheroli.core.util.RequestUtil;
import cn.com.gzheroli.tag.core.DataGrid;
import cn.com.gzheroli.util.GenSqlUtil;
import net.ddns.heroli.pojo.TBBrand;
import net.ddns.heroli.pojo.TBGoods;
import net.ddns.heroli.pojo.TBGoodsAttr;
import net.ddns.heroli.pojo.TBModel;
import net.ddns.heroli.pojo.TBShop;

/**  
* 类名: NoAuthController  
* 描述: 免登陆的一些客户端访问方法 
* 作者: 李长荣 lichang_20011@163.com  
* 日期: 2017年12月22日  上午10:08:40
*@version V1.0  
*    
*/
@Controller
@RequestMapping("/api/noAuthController")
public class NoAuthController {
	private static final Logger logger = LoggerFactory.getLogger(NoAuthController.class);
	@Autowired
	private CommonService commonService;
	@Autowired
	private HeroService heroService;
	@Autowired
    private SpringRedisCache cache;
	@Autowired
	private SystemService systemService;
	@RequestMapping(value = "getGoodsTypeTree")
	@ResponseBody
	public AjaxJson getGoodsTypeTree(HttpServletRequest req,HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		String sql="select  * from T_B_GOODS_TYPE where PARENT_ID is null and STATUS='1'";
		List<Map<String,Object>> treeMapList=new ArrayList<Map<String,Object>>();
		Map para=new HashMap();
		List<Map<String,Object>> treeList=commonService.findForJdbc(sql, para);
		for(Map<String,Object> t:treeList){
			Map<String,Object> m=new HashMap<String,Object>();
			m.put("label", t.get("name"));
			m.put("name", t.get("name"));
			m.put("code", t.get("code"));
			m.put("id", t.get("id"));
			m.put("remark", t.get("remark"));
			m.put("type", "");
			List childList = new ArrayList();
			m.put("children", getGoodsTypeChild(t.get("id").toString(),childList));
			treeMapList.add(m);
		}
		j.setObj(treeMapList);
		return j;
	}
	private List<Map<String,Object>> getGoodsTypeChild(String id,List<Map<String,Object>> menuList){
		String sql="select  * from T_B_GOODS_TYPE where PARENT_ID=#{parentId} and STATUS='1'";
		List<Map<String,Object>> treeMapList=new ArrayList<Map<String,Object>>();
		Map para=new HashMap();
		para.put("parentId", id);
		List<Map<String,Object>> treeList=commonService.findForJdbc(sql, para);
		for(Map<String,Object> t:treeList){
			Map<String,Object> m=new HashMap<String,Object>();
			m.put("label", t.get("name"));
			m.put("name", t.get("name"));
			m.put("code", t.get("code"));
			m.put("id", t.get("id"));
			m.put("remark", t.get("remark"));
			m.put("type", "");
			m.put("parentId", id);
			List childList = new ArrayList();
			m.put("children", getGoodsTypeChild(t.get("id").toString(),childList));
			treeMapList.add(m);
		}
		return treeMapList;
	}
	@RequestMapping(value = "getBrandList")
	@ResponseBody
	public  AjaxJson  getBrandList(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		if(reqMap.containsKey("url"))
		conMap.put("url", "=");
		if(reqMap.containsKey("isHot"))
			conMap.put("isHot", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBBrand.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String sql=GenSqlUtil.genSqlByClass(clazz, conMap,"list",tableColumns,"am",reqMap);
		List beanList =commonService.findForJdbcPage(sql, clazz, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(beanList);
		String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		Long total=commonService.getCountForJdbc(countSql, reqMap);
		j.setTotal(total);
		return j;
	}
	@RequestMapping(value = "getModelList")
	@ResponseBody
	public  AjaxJson  getModelList(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		if(reqMap.containsKey("brandId"))
		conMap.put("brandId", "=");
		if(reqMap.containsKey("catId"))
			conMap.put("catId", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		if(reqMap.containsKey("desc"))
			conMap.put("desc", "like");
		if(reqMap.containsKey("catName"))
			conMap.put("catName", "like");
		Class clazz=TBModel.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String sql=GenSqlUtil.genSqlByClass(clazz, conMap,"list",tableColumns,"am",reqMap);
		List beanList =commonService.findForJdbcPage(sql, clazz, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(beanList);
		String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		Long total=commonService.getCountForJdbc(countSql, reqMap);
		j.setTotal(total);
		return j;
	}
	@RequestMapping(value = "getShopList")
	@ResponseBody
	public  AjaxJson  getShopList(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		if(reqMap.containsKey("proId"))
		conMap.put("proId", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBShop.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String select="s.*,bu.REAL_NAME,bu.USER_NAME,u.*";
		String sql="  from T_B_SHOP s,T_S_BASE_USER bu,T_S_USER u ";
		sql=sql+" where s.ID=bu.ID and bu.ID=u.ID and bu.STATUS='1' ";
		DataGrid grid=heroService.getDataGridReturn(sql, reqMap,select,Map.class, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(grid.getResults());
		j.setTotal(Long.parseLong(grid.getTotal()+""));
		return j;
	}
	@RequestMapping(value = "getGoodsAttr")
	@ResponseBody
	public  AjaxJson  getGoodsAttr(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		//if(reqMap.containsKey("projectType"))
		//conMap.put("projectType", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBGoodsAttr.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String sql=GenSqlUtil.genSqlByClass(clazz, conMap,"list",tableColumns,"am",reqMap);
		List beanList =commonService.findForJdbcPage(sql, clazz, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(beanList);
		String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		Long total=commonService.getCountForJdbc(countSql, reqMap);
		j.setTotal(total);
		return j;
	}
	@RequestMapping(value = "getGoods")
	@ResponseBody
	public  AjaxJson  getGoods(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		//if(reqMap.containsKey("projectType"))
		//conMap.put("projectType", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBGoods.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String sql=GenSqlUtil.genSqlByClass(clazz, conMap,"list",tableColumns,"am",reqMap);
		List beanList =commonService.findForJdbcPage(sql, clazz, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(beanList);
		String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		Long total=commonService.getCountForJdbc(countSql, reqMap);
		j.setTotal(total);
		return j;
	}
}
